<template>
	<div class="mall_goods_cart">
		<MallGoodsDetailNav :title="title" />
		<MallCartGoodsList :cartGoods="cartGoods" @setBuyGoods="setBuyGoods" @setCartGoods="setCartGoods" :selectAllGoods="selectAllGoods" />
		<MallGoodsCartTools :priceTotal="priceTotal" :buyGoods="buyGoods" :goodsCount="buyGoodsCount" @setSelectAllGoods="setSelectAllGoods" :selectAllGoods="selectAllGoods" v-if="cartGoods.length>0" />
	</div>
</template>
<script>
	export default{
		name: "MallGoodsAddCart",
		data:function(){
			let goods = this.getAddCartGoods();
			return{
				title: this.$route.meta.title,
				cartGoods: goods,
				goodsCount: 0,
				priceTotal: 0,
				buyGoods: [],
				buyGoodsCount: 0,
				selectAllGoods: false
			}
		},
		created: function(){
			this.$emit("upLocalData", [{
				name: "type",
				value: "mallGoodsCart"
			},{
				name: "channelName",
				value: this.$route.meta.title
			}]);
		},
		mounted:function(){
			this.getGoodsCount();
		},
		watch:{
			selectAllGoods: function(v){
				let goods = this.getAddCartGoods(v);
				this.$set(this, "cartGoods", goods);
			}
		},
		methods:{
			setCartGoods: function(v){
				this.$set(this, "cartGoods", v);
				this.getGoodsCount();
			},
			setBuyGoods:function(v, n, p, s){
				//console.log(v, n)
				this.$set(this, "buyGoods", v);
				this.$set(this, "buyGoodsCount", n);
				this.$set(this, "priceTotal", p);
				this.$set(this, "selectAllGoods", s);
			},
			setSelectAllGoods:function(v){
				this.$set(this, "selectAllGoods", v);
				let cartGoods = this.$store.state.cartGoods;
				if (typeof v !== "undefined"){
					let s = 0, p = 0, b=[];
					cartGoods.each((i, item) => {
						let c = [];
						cartGoods[i].goods.each((n, t) => {
							cartGoods[i].goods[n].checked = v;
							if (v) {
								p+=parseFloat(t.price) * parseInt(t.buyNum);
							}
						});
						cartGoods[i].checked = v;
						if (v) {
							s+=cartGoods[i].goods.length;
						}
					});
					this.$set(this, "buyGoods", v ? cartGoods : b);
					this.$set(this, "buyGoodsCount", s);
					this.$set(this, "priceTotal", p);
				}
				this.$store.commit("setCartGoods", cartGoods);
			},
			getGoodsCount(){
				let count = 0;
				this.cartGoods.each((i, item) => {
					count+=item.goods.length;
				});
				this.$set(this, "goodsCount", count);
				this.$set(this, "title", this.$route.meta.title+"("+count+")");
			},
			getAddCartGoods(v){
				let goods = [],
					that = this;
				this.$store.state.goods.each((i, item) => {
					if (parseFloat(that.$route.params.id) === item.id && parseFloat(that.$route.params.cid) === item.cid){
						goods.push(item);
						return false;
					}
				});
				let cartGoods = this.$store.state.cartGoods;
				goods.each((i, item) => {
					//console.log(item.shopid)
					let a = extend({}, {
						shop: that.getShops(item.shopid) || {},
						shopid: item.shopid,
						checked: false,
						isEdit: false,
						goods: []
					});
					let bool = that.diff(cartGoods, "shopid", parseFloat(item.shopid));
					if (bool === null){
						cartGoods.push(extend(a, {
							goods: (a.goods.push(extend(item, {
								buyNum: 1,
								isEdit: false,
								checked: false
							})), a.goods)
						}));
					} else {
						cartGoods[bool].goods.indexOf(item) < 0 && cartGoods[bool].goods.push(extend(item, {
							buyNum: 1,
							isEdit: false,
							checked: false
						}));
					}
				});
				this.$store.commit("setCartGoods", cartGoods);
				return cartGoods;
			},
			diff(arr, name, value){
				let bool = null;
				arr.each((i, item) => {
					//console.log(item, name, item[value], value)
					if (item[name] === value){
						bool = i;
					}
				});
				return bool;
			},
			getShops(cid){
				let goods = {},
					that = this;
				this.$store.state.shops.each((i, item) => {
					if (parseFloat(cid) === parseFloat(item.shopid)){
						goods = item;
						return false;
					}
				});
				return goods;
			}
		},
		components:{
			LazyImg: r => require.ensure([], () => r(require('../../components/common/lazy-img')), 'LazyImg'),
			MallGoodsDetailNav: r => require.ensure([], () => r(require('../../components/mall/mall-goods-detail-nav')), 'MallGoodsDetailNav'),
			MallCartGoodsList: r => require.ensure([], () => r(require('../../components/mall/mall-cart-goods-list')), 'MallCartGoodsList'),
			MallGoodsCartTools: r => require.ensure([], () => r(require('../../components/mall/mall-goods-cart-tools')), 'MallGoodsCartTools')
		}
	}
</script>
<style lang="less">
	@base:23.44/1rem;
	.mall_goods_cart{
		padding:0 0 80/@base 0;
	}
</style>